package com.example.win.android_banduyujia.peopleactivity.ui.mapView;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Win on 2015/9/6.
 */
public class MyApplication extends Application {

    static MyApplication myApplication;
    String mStrKey = "BB4FEFE6EE3ADDA18993E36DC6A4F4827F30C604";
    boolean   m_bKeyRight = true; // 授权Key正确，验证通过
    private List<Activity>  activityList = new LinkedList<Activity>();
    public BMapManager mBMapManager= null;;

    @Override
    public void onCreate() {
        myApplication = this;
        initEngineManager(this);
        super.onCreate();
    }

    private void initEngineManager(Context context) {
        if(mBMapManager == null){
            mBMapManager = new BMapManager(context);
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager 初始化sucsuc!", Toast.LENGTH_LONG).show();
        }
        if(!mBMapManager.init(mStrKey, new MyGeneralListener())){
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager 初始化错误!", Toast.LENGTH_LONG).show();
        }
    }

    public static MyApplication getInstance() {
        if(myApplication == null){
            myApplication = new MyApplication();
        }
        return myApplication;
    }

    public void addActivity(Activity activity){
         activityList.add(activity);
    }

    public void exit(){
        for(Activity activity : activityList){
            activity.finish();
        }
        System.exit(0);
    }

    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    static class MyGeneralListener implements MKGeneralListener{

        @Override
        public void onGetNetworkState(int i) {
            if(i == MKEvent.ERROR_NETWORK_CONNECT){
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), "你的网络出错啦！", Toast.LENGTH_LONG).show();
            }else if(i == MKEvent.ERROR_NETWORK_DATA){
                Toast.makeText(MyApplication.getInstance().getApplicationContext(),"输入正确的搜索条件!",Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onGetPermissionState(int i) {
             if(i == MKEvent.ERROR_PERMISSION_DENIED){
                 // 授权Key错误：
                 Toast.makeText(MyApplication.getInstance().getApplicationContext(), "请在 DemoApplication.java文件输入正确的授权Key！", Toast.LENGTH_LONG).show();
                 MyApplication.getInstance().m_bKeyRight = false;
             }
        }
    }
}
